package tianwang.biz.stat;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;

import tianwang.model.AticleModel;
import tianwang.model.CAStyle;
import tianwang.services.collect.CollectResult;
import tianwang.services.collect.Collecter;

/**
 * 句长的标准差
 * 
 * @author tian
 *
 */
public class SentenceStandardDeviation implements Collecter<CollectResult> {
	private final String name = CAStyle.STANDARD_DEVIATION_SENTENCE_LENGTH;

	@Override
	public CollectResult collect(Object material) throws Exception {
		AticleModel model = (AticleModel) material;
		ArrayList<ArrayList<String>> list = model.getPunctuatorWords();
		
		List<Double> samples =new ArrayList<Double>();
		
		int len = list.size();
		for(int i = 0; i < len; i++){
			int s = list.get(i).size();
			if( s == 0){
				continue;
			}
			samples.add(s + 0.0); 
		}		
		double[] arr = new double[samples.size()];
		len = samples.size();
		for(int i = 0; i < len; i++){
			arr[i] = samples.get(i);
		}
		
		StandardDeviation sd  = new StandardDeviation();
		
		final int re = (int) Math.round(sd.evaluate(arr))/*StatUtils.variance(arr)*/;
		
		return new CollectResult(){
			public Object pump(){
				return re;
			}

			@Override
			public String getName() {
				return name;
			}
		};
	}

	
}
